define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goodsbrand/index',
                    add_url: 'goodsbrand/add',
                    edit_url: 'goodsbrand/edit',
                    del_url: 'goodsbrand/del',
                    multi_url: 'goodsbrand/multi',
                    table: 'goods_brand',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'brand_id',
                sortName: 'brand_id',
                columns: [
                    [
                        {field: 'brand_id', title: __('Brand_id')},
                        {field: 'brand_title', title: __('Brand_title')},
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            formatter: Controller.api.formatter.custom
                        },
                    ]
                ]
            });

            $(table).on("click", "[data-id].btn-delme", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                var that = this;
                Layer.confirm(
                        __('确定删除品牌吗？'),
                        {icon: 3, title: __('确定删除品牌吗？'), shadeClose: true},
                        function (index) {
                            $.post('goodsbrand/delme', {id: id}, function (res) {
                                table.bootstrapTable('refresh', {});
                            });
                            Layer.close(index);
                        }
                );
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                custom: function (value, row, index) {
                    if (row.brand_id == 1000000) {
                        return '-';
                    } else {
                        //添加上btn-change可以自定义请求的URL进行数据处理
                        var html = '<a href="goodsbrand/edit/ids/' + row.brand_id + '" class="btn btn-info btn-xs btn-add btn-dialog" title="编辑" data-table-id="table"><i class="fa fa-pencil"></i> 编辑</a>';
                        html += '&nbsp;<a class="btn  btn-xs  text-success btn-danger btn-delme"  style="cursor:pointer" data-url="goodsbrand/delme" data-id="' + row.brand_id + '"><i class="fa fa-trash"></i>删除</a>';
                        return html;
                    }

                },
            }
        }
    };
    return Controller;
});